/*
 * Copyright 2007 Matt Jensen
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package org.jtell.internal;

import org.jtell.EventMetadata;

import java.util.Map;

/**
 * <p>
 * <code>EventMetadataImpl</code> is the default implementation of the {@link EventMetadata} interface.
 * </p>
 * <p>
 * <strong>Thread Safety</strong><br/>
 * Instances of this class are safe for multithreaded access. 
 * </p>
 */
public class EventMetadataImpl implements EventMetadata
{
    /**
     * <p>
     * The additional attributes. 
     * </p>
     */
    private final Map<String, String> m_attributes;

    /**
     * <p>
     * The fully qualified event class name. 
     * </p>
     */
    private final String m_eventClassName;

    /**
     * <p>
     * Precalculated hash code. 
     * </p>
     */
    private final int m_hashCode;

    /**
     * <p>
     * The fully qualified source class name. 
     * </p>
     */
    private final String m_sourceClassName;

    /**
     * <p>
     * Construct a {@link EventMetadataImpl} instance.
     * </p>
     *
     * @param eventClassName the fully qualified event class name.
     * @param sourceClassName the fully qualified source class name.
     * @param attributes the additional attribute map.
     */
    public EventMetadataImpl(final String eventClassName,
                             final String sourceClassName,
                             final Map<String, String> attributes)
    {
        super();
        Guard.notNull("eventClassName", eventClassName);
        Guard.notNull("sourceClassName", sourceClassName);
        Guard.notNull("attributes", attributes);

        m_attributes = attributes;
        m_eventClassName = eventClassName;
        m_sourceClassName = sourceClassName;
        m_hashCode = calculateHashCode();
    }

    /* inherit javadoc */
    public boolean equals(Object o)
    {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        EventMetadataImpl that = (EventMetadataImpl) o;

        if (m_hashCode != that.m_hashCode) return false;
        if (!m_attributes.equals(that.m_attributes)) return false;
        if (!m_eventClassName.equals(that.m_eventClassName)) return false;
        if (!m_sourceClassName.equals(that.m_sourceClassName)) return false;

        return true;
    }

    /* inherit javadoc */
    public int hashCode()
    {
        return m_hashCode;
    }

    /* inherit javadoc */
    public Map<String, String> getAllAttributes()
    {
        return m_attributes;
    }

    /* inherit javadoc */
    public String getAttribute(final String name)
    {
        final String result;
        if (!m_attributes.containsKey(name))
        {
            result = null;
        }
        else
        {
            result = m_attributes.get(name);
        }
        return result;
    }

    /* inherit javadoc */
    public String getEventClassName()
    {
        return m_eventClassName;
    }

    /* inherit javadoc */
    public String getSourceClassName()
    {
        return m_sourceClassName;
    }

    /**
     * <p>
     * Calculate the instance hash code.
     * </p>
     *
     * @return <code>int</code> hash code.
     */
    private int calculateHashCode()
    {
        int result;
        result = m_attributes.hashCode();
        result = 31 * result + m_eventClassName.hashCode();
        result = 31 * result + m_sourceClassName.hashCode();
        return result;
    }
}
